<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>SuperScope | 注册</title>
  <!-- 告诉浏览器屏幕自适应 -->
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <!-- Font Awesome -->
  <link rel="stylesheet" href="../../static/plugins/fontawesome-free/css/all.min.css">
  <!-- Ionicons -->
  <link rel="stylesheet" href="https://cdn.bootcss.com/ionicons/2.0.1/css/ionicons.min.css">
  <!-- icheck bootstrap -->
  <link rel="stylesheet" href="../../static/plugins/icheck-bootstrap/icheck-bootstrap.min.css">
  <!-- 主题样式 -->
  <link rel="stylesheet" href="../../static/dist/css/adminlte.min.css">
  <!-- 离线 Google 字体: Source Sans Pro -->
  <link href="../../static/dist/css/google.css?family=Source+Sans+Pro:300,400,400i,700" rel="stylesheet">
    <link rel="stylesheet" href="../../static/plugins/toastr/toastr.min.css">
</head>
<body class="hold-transition register-page">
<div class="register-box">
  <div class="register-logo">
    <a href="#"><b>Super</b>Scope</a>
  </div>

  <div class="card">
    <div class="card-body register-card-body">
      <p class="login-box-msg">注册新会员</p>
        <div class="input-group mb-3">
          <input name="userid" id="userid" type="text" class="form-control" placeholder="学号">
          <div class="input-group-append">
            <div class="input-group-text">
              <span class="fas fa-user"></span>
            </div>
          </div>
        </div>
        <div class="input-group mb-3">
          <input name="username" id="username" type="text" class="form-control" placeholder="全名">
          <div class="input-group-append">
            <div class="input-group-text">
              <span class="fas fa-user"></span>
            </div>
          </div>
        </div>
        <div class="input-group mb-3">
          <input name="phone" id="phone" type="text" class="form-control" placeholder="电话">
          <div class="input-group-append">
            <div class="input-group-text">
              <span class="fas fa-phone-square"></span>
            </div>
          </div>
        </div>
        <div class="input-group mb-3">
          <input name="dormitory" id="dormitory" type="text" class="form-control" placeholder="宿舍">
          <div class="input-group-append">
            <div class="input-group-text">
              <span class="fas fa-building"></span>
            </div>
          </div>
        </div>
        <div class="input-group mb-3">
          <input name="password" id="password" type="password" class="form-control" placeholder="密码">
          <div class="input-group-append">
            <div class="input-group-text">
              <span class="fas fa-lock"></span>
            </div>
          </div>
        </div>
        <div class="input-group mb-3">
          <input name="passwordAgain" id="passwordAgain" type="password" class="form-control" placeholder="再次输入密码">
          <div class="input-group-append">
            <div class="input-group-text">
              <span class="fas fa-lock"></span>
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col-8">
            <div class="icheck-primary">
            </div>
          </div>
          <!-- /.col -->
          <div class="col-4">
            <button id="register" type="button" class="btn btn-primary btn-block">注册</button>
          </div>
          <!-- /.col -->
        </div>
      <a href="{{ url_for('login') }}" class="text-center">我已经有账号了</a>
    </div>
    <!-- /.form-box -->
  </div><!-- /.card -->
</div>
<!-- /.register-box -->

<!-- jQuery -->
<script src="../../static/plugins/jquery/jquery.min.js"></script>
<!-- Bootstrap 4 -->
<script src="../../static/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- AdminLTE App -->
<script src="../../static/dist/js/adminlte.min.js"></script>
<script src="../../static/plugins/toastr/toastr.min.js"></script>
<script type = "text/javascript" >
    $('#register').on('click',
  function() {
        var userid = $('#userid').val();
        var username = $('#username').val();
        var phone = $('#phone').val();
        var dormitory = $('#dormitory').val();
        var password = $('#password').val();
        var passwordAgain = $('#passwordAgain').val();

        var data = {
            data:JSON.stringify({
                'userid':userid,
                'username':username,
                'phone':phone,
                'dormitory':dormitory,
                'password':password,
            }),
        }

        //检测部分
        if(userid === "" || username === "" || phone === "" || dormitory === "" || password === ""){
            toastr.error('不能有数据为空');
            return ;
        }

        if(password !== passwordAgain) {
            toastr.error('两次密码不一致!');
            return ;
        }

        if(phone.length !== 11){
            toastr.error('请输入正确的电话号码');
            return ;
        }

        if(userid.length !== 10){
            toastr.error('请输入正确的学号');
            return ;
        }

        //提交
        $.ajax({
            type:"POST",
            dataType:"json",
            url:"/login/register",
            data: data,
            success :function (result){
                console.log(result);
                if(result === "成功注册"){
                    toastr.success("成功注册,3s后跳转");
                    setTimeout(function (){window.location.href = '/login/login'},3000);
                }
                else{
                    if(result === "学号已经存在")
                        toastr.error("学号已经存在");
                    else{
                        if(result === "宿舍已满")
                            toastr.error("宿舍已满");
                        else{
                            if(result === "宿舍不存在")
                                toastr.error("宿舍不存在");
                            else
                                toastr.error("未知错误!请联系管理员");
                        }
                    }
                }
            },
            error:function (result){
                console.log(result);
                toastr.error('未知错误!请联系管理员');
            }
        });
  })
</script>
</body>
</html>
